#include <iostream>
#include <iomanip>
#include <map>
#include <cstring>
using namespace std;

//insert by insert() search by find()
//N * log( char_map.size() ) + M * log( char_map.size() )
const char * strpbrk_last(const char * message, const char * charset)
{
  map<char,int> char_map;
  map<char,int>::iterator iter;
 //put into a map container N*log( char_map.size() )
  for(int i = strlen(message)-1; i>=0; i--){
    char_map.insert(make_pair(message[i],i) );
  }

  int last = -1; //last occurence
  //find in char_map M*log( char_map.size() )
  for(unsigned int i = 0; i<strlen(charset); i++) { 
    iter = char_map.find(charset[i]);
    if(iter != char_map.end() && last < iter->second)
      last = iter->second;
  }

  if(last >= 0)
    return message+last;
  else
    return NULL;
}
int main(int argc, char *argv[])
{
  char message[] = "Life is an extreme sport! Focus! Drive! Excel!";
  char charset[] = "iueo";


  cout<<setfill('-')<<setw(20)<<""<<endl;
  const char * ptr = strpbrk_last(message, charset);
 
  if(ptr)
    cout<<"the last char from: "<<endl
	<<message<<endl
	<<"in char set: "<<endl
	<<charset<<endl
	<<"is: "<<*ptr<<endl;
  else
    cout<<"not found"<<endl;
  return 0;
}
